package com.group.library.order.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.group.library.order.dao.AdminBaseMapper;
import com.group.library.order.entity.Admin;
import com.group.library.order.service.AdminBaseService;
import com.group.library.order.util.MessageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminBaseServiceImpl implements AdminBaseService {

    @Autowired
    private AdminBaseMapper adminBaseMapper;

    @Override
    public Admin findAdmin(String name, String password) {
        return adminBaseMapper.findAdmin(name,password);
    }

    @Override
    public PageInfo<Admin> findAllAdmin(Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        List<Admin> admins = adminBaseMapper.findAllAdmin();
        return new PageInfo<Admin>(admins);
    }

    @Override
    public Boolean addAdmin(String adminName, String adminPwd) {
        try{
            Admin admin = new Admin();
            admin.setAdminId(null);// 自动递增
            admin.setAdminName(adminName);
            admin.setAdminPwd(adminPwd);
            admin.setAdminType(false);//在这里创建的只能是子管理员，超级管理员需要在数据库中创建，且最先创建
            if(adminBaseMapper.addAdmin(admin)==1)//添加成功！
                return true;
            return false;
        } catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
}
